/*

 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.core;

import hcmus.fit.master.sevensea.utils.GameUtils;

import java.util.ArrayList;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

/**
 * 
 * @author hvp
 */
public abstract class AnimationSprite implements Sprite {
    protected List<Bitmap> frames;
    protected int index;
    protected Point pos;
    protected Point viewPos;
    protected Paint paint;
    protected int numberOfFrame;
    protected Resources resources;

    public Point getPos() {
        return pos;
    }

    public void setPos(Point pos) {
        this.pos = pos;
    }

    public Point getViewPos() {
        return viewPos;
    }

    public void setViewPos(Point viewPos) {
        this.viewPos = viewPos;
    }

    public Paint getPaint() {
        return paint;
    }

    public int getIndex() {
        return index;
    }

    public int getNumberOfFrame() {
        return numberOfFrame;
    }

    public AnimationSprite(Resources resources, Point p) {
        this.resources = resources;
        this.index = 0;
        this.numberOfFrame = getNbOfCol() * getNbOfRow();
        this.paint = new Paint();
        this.pos = new Point(p);
        this.viewPos = translateToView(this.pos);
        this.frames = getFrames();
    }

    public void moveImmediate(Point newPOs) {
        this.pos.set(newPOs.x, newPOs.y);
    }

    @Override
    public void draw(Canvas canvas) {
        int w = frames.get(index).getWidth() - 1;
        int h = frames.get(index).getHeight() - 1;
        canvas.drawBitmap(frames.get(index), new Rect(0, 0, w, h), new Rect(
                viewPos.x, viewPos.y, viewPos.x + w, viewPos.y + h), paint);
    }

    @Override
    public void update() {
        index = (index + 1) % numberOfFrame;
    }

    /**
     * Translate sprite position from MAP to view
     * 
     * @param p
     * @return
     */
    public Point translateToView(Point p) {
        return new Point(p.x * Constant.CellWidth, p.y * Constant.CellHeight);
    }

    protected List<Bitmap> getFrames() {
        Bitmap res = BitmapFactory.decodeResource(this.resources,
                getResourceId());
        return createSpriteImage(
                GameUtils.createTransparentBitmapFromBitmap(res, Color.BLACK),
                getNbOfCol(), getNbOfRow());
    }

    protected List<Bitmap> createSpriteImage(Bitmap res, int maxX, int maxY) {
        List<Bitmap> result = new ArrayList<Bitmap>();
        int w = res.getWidth() / maxX;
        int h = res.getHeight() / maxY;

        for (int i = 0; i < maxX; i++) {
            for (int j = 0; j < maxY; j++) {
                result.add(Bitmap.createBitmap(res, i * w, j * h, w, h));
            }
        }
        return result;
    }

    protected abstract int getResourceId();

    protected abstract int getNbOfCol();

    protected abstract int getNbOfRow();
    // protected abstract boolean isShip();
    // protected abstract boolean isIsland();
    // protected abstract boolean isWhirl();
    // protected abstract boolean isRubble();
    // protected abstract boolean isEnemy();

}
